<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;

class PersonalUser extends BaseController
{
    //重置个人密码
    public function resetPassword()
    {
        $userId = $this->request->auth['data']->user->user_id;
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data = Request::param('data');
        try {
            Db::startTrans();
            // 更新用户密码
            Db::table('user')->where('user_id', $userId)->update(['user_password' => md5($data['user_password'])]);

            Db::commit();
            return json(['code' => 200, 'msg' => '密码重置成功']);
        } catch (Exception $e) {
            Db::rollback();
            return json(['code' => 400, 'msg' => '密码重置失败'.$e->getMessage()]);
        }
        
    }

    //获取个人信息
    public function getPersonalInfo()
    {
        $userId = $this->request->auth['data']->user->user_id;
        $userInfo = Db::name('user')->where('user_id', $userId)->find();
        //选择性获取
        $userData= [
            'user_username' => $userInfo['user_username'],
            'user_name' => $userInfo['user_name'],
            'user_phone' => $userInfo['user_phone'],
            'user_sex' => $userInfo['user_sex']==1 ? '男' : '女',
            'user_company_id' => $userInfo['user_company_id'],
            'user_creationtime' => $userInfo['user_creationtime']
            ];
        return json(['code' => 200, 'msg' => '获取个人信息成功', 'data' => $userData]);
    }


}